home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / axdata / timer.cls < prev   
Encoding:
Visual Basic class definition  |  1998-10-21  |  3.0 KB  |  87 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "objTimer"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = False
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. Attribute VB_Ext_KEY = "SavedWithClassBuilder" ,"Yes"
  11. Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
  12. '-------------------------------------------------------------------------------
  13. ' Copyright ⌐ 1997 Microsoft Corporation. All rights reserved.
  14. '
  15. ' You have a royalty-free right to use, modify, reproduce and distribute the
  16. ' Sample Application Files (and/or any modified version) in any way you find
  17. ' useful, provided that you agree that Microsoft has no warranty, obligations or
  18. ' liability for any Sample Application Files.
  19. '-------------------------------------------------------------------------------
  20.  
  21. '-------------------------------------------------------------------------------
  22. ' Timer object code has been demonstrated before. This implementation contains
  23. ' one slight improvement over some other implementations: It's global collection
  24. ' of timer objects is a keyed list. The items are pointers to the timer objects.
  25. ' The keys are timer IDs (returned by StartTimer). This enables the TimerProc in
  26. ' modTimer to very quickly find the appropriate timer object, given a timer ID.
  27. '-------------------------------------------------------------------------------
  28.  
  29. Option Explicit
  30.  
  31. Public Event Timer()
  32.  
  33. Private Const mnDefaultInterval As Long = 1
  34.  
  35. Private mnTimerID As Long
  36. Private mnInterval As Long
  37. Private mfEnabled As Boolean
  38.  
  39. Public Property Get Interval() As Long
  40.     Interval = mnInterval
  41. End Property
  42. Public Property Let Interval(Value As Long)
  43.     If mnInterval <> Value Then
  44.         mnInterval = Value
  45.         If mfEnabled Then
  46.             SetInterval mnInterval, mnTimerID
  47.         End If
  48.     End If
  49. End Property
  50.  
  51. Public Property Get Enabled() As Boolean
  52.     Enabled = mfEnabled
  53. End Property
  54. Public Property Let Enabled(Value As Boolean)
  55.     If mfEnabled <> Value Then
  56.         If Value Then
  57.             mnTimerID = StartTimer(mnInterval)
  58.             If mnTimerID <> 0 Then
  59.                 mfEnabled = True
  60.                 'Storing Me in the global would add a reference to Me, which
  61.                 '   would prevent Me from being released, which in turn would
  62.                 '   prevent my Class_Terminate code from running. To prevent
  63.                 '   this, I store a "soft reference" - the collection holds a
  64.                 '   pointer to me without incrementing my reference count.
  65.                 gcTimerObjects.Add ObjPtr(Me), mnTimerID
  66.             End If
  67.         Else
  68.             StopTimer mnTimerID
  69.             mfEnabled = False
  70.             gcTimerObjects.Remove mnTimerID
  71.         End If
  72.     End If
  73. End Property
  74.  
  75. Private Sub Class_Initialize()
  76.     If gcTimerObjects Is Nothing Then Set gcTimerObjects = New SortedList
  77.     mnInterval = mnDefaultInterval
  78. End Sub
  79.  
  80. Private Sub Class_Terminate()
  81.     Enabled = False
  82. End Sub
  83.  
  84. Friend Sub Tick()
  85.     RaiseEvent Timer
  86. End Sub
  87.